package fi.hell.pollit.domain

import fi.hell.pollit.util.MailerService;

class VoteController {

    MailerService mailerService

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
//
//        Map<String, String> settings = [
//            "to":"jukka.hell@symmetria.fi",
//            "subject":"test from pollit",
//            "plainTextMessage":"Testiä testiä puhutteks te suomee"
//        ]
//        try {
//            println mailerService.sendEmail(settings)
//        } catch (Exception e) {
//            println "ERROR: ${e.getMessage()}"
//        }
//        

        [voteInstanceList: Vote.list(params), voteInstanceTotal: Vote.count()]
    }

    def create = {
        def voteInstance = new Vote()
        voteInstance.properties = params
        return [voteInstance: voteInstance]
    }

    def save = {
        User loggedInUser = User.getLoggedInUser();
        Poll poll = Poll.get(params.poll.id);
        params.ip = request.getRemoteAddr();

        if (poll?.allowedToVote(loggedInUser, params.ip)) {
            List<Long> votes = params.list("selection.id");
            params.remove("selection.id");
            boolean errors = false;

            for (Long selectionId in votes) {
                def voteInstance = new Vote(params)
                voteInstance.selection = Opt.get(selectionId as long);

                if (voteInstance.save(flush: true)) {
                    flash.message = "${message(code: 'default.created.message', args: [message(code: 'vote.label', default: 'Vote'), voteInstance.id])}"
                }
                else {
                    flash.errors = "${message(code: 'vote.saveFailed', default: 'Failed to save your vote.')}"
                }

                // Allow only one selection if not multivote poll
                if (!poll.multivote) {
                    break;
                }
            }

            redirect(url:grailsApplication.config.grails.serverURL);
        } else {
            flash.error = "${message(code: 'vote.notLoggedIn', default:"Sorry, your not allowed to vote this poll. You're already voted, or your not in the allowed users list.")}"
            redirect(url:grailsApplication.config.grails.serverURL);
        }
    }

    def show = {
        def voteInstance = Vote.get(params.id)
        if (!voteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            redirect(action: "list")
        }
        else {
            [voteInstance: voteInstance]
        }
    }

    def edit = {
        def voteInstance = Vote.get(params.id)
        if (!voteInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [voteInstance: voteInstance]
        }
    }

    def update = {
        def voteInstance = Vote.get(params.id)
        if (voteInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (voteInstance.version > version) {
                    
                    voteInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'vote.label', default: 'Vote')] as Object[], "Another user has updated this Vote while you were editing")
                    render(view: "edit", model: [voteInstance: voteInstance])
                    return
                }
            }
            voteInstance.properties = params
            if (!voteInstance.hasErrors() && voteInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'vote.label', default: 'Vote'), voteInstance.id])}"
                redirect(action: "show", id: voteInstance.id)
            }
            else {
                render(view: "edit", model: [voteInstance: voteInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def voteInstance = Vote.get(params.id)
        if (voteInstance) {
            try {
                voteInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'vote.label', default: 'Vote'), params.id])}"
            redirect(action: "list")
        }
    }
}
